package com.ermao.controller;

import com.ermao.pojo.User;
import com.ermao.service.BudgetService;
import com.ermao.utils.Result;
import com.ermao.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 用户预算模块
 * @author Ermao
 * Date: 2021/8/31 0:46
 */
@RestController
@RequestMapping("/api/v1/users/budget")
public class BudgetController {

	private BudgetService budgetService;

	@Autowired
	public void setBudgetService(BudgetService budgetService) {
		this.budgetService = budgetService;
	}

	@PutMapping("")
	public Result modifyBudget(@RequestParam("userId") Integer userId,
							@RequestParam("budgetAmount") Integer budgetAmount) {
		if (budgetService.updateBudget(userId, budgetAmount)) {
			return ResultGenerator.genSuccessResult();
		} else {
			return ResultGenerator.genFailResult("修改预算失败");
		}
	}

	@GetMapping("/{userId}")
	public Result getBudget(@PathVariable("userId") Integer userId) {
		User user = budgetService.selectBudgetByUserUd(userId);
		if (user != null) {
			return ResultGenerator.genSuccessResult(user);
		} else {
			return ResultGenerator.genFailResult("查询用户预算失败");
		}
	}
}
